home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2160 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  80 lines

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with c code, please help!
  5. Date: 19 Jan 1996 07:51 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <19JAN199607513990@erich.triumf.ca>
  9. References: <surgsw-1901960148530001@128.206.206.86>
  10. NNTP-Posting-Host: erich.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <surgsw-1901960148530001@128.206.206.86>, surgsw@mizzou1.missouri.edu (Joel Weinstein) writes...
  14. >I have been trying to get this very simple piece of code to work for
  15. >hours.  What is the problem???????
  16. >#include <stdio.h>
  17. >main()
  18. >{
  19. >   int i=0;
  20. >   char  word[100], c;
  21. >   printf("Enter a word:   ");
  22. >   while( (c = getchar()) != '\n')  {
  23. >      *word = c;
  24. >      word == word + 1;
  25.  
  26. word is the name of an array, and cannot be incremented or otherwise changed -
  27. if it could, you would lose access to the array. The name of an array can often
  28. be treated as a const pointer to the first  element of an array.
  29.  
  30. word == word+1;  _compares_ word to word+1 - it doesn't do an assignment.
  31.  
  32. To do what you want, you need a _real_ char pointer variable like:
  33.  
  34.    int i = 0;
  35.    char word[100], c;
  36.    char* temp;
  37.  
  38.    temp = word;   /* set temp to point to the start of the array  */
  39.    while((c = getchar()) != '\n') {
  40.      *temp++ = c;
  41.      }
  42.      *temp = '\0';
  43.      temp = word;  /* reset temp to the start of the array  */
  44. /*  and use temp instead of word below...  */
  45.  
  46. >  
  47. >   printf("You entered:  ");  
  48. >   while( *word != '\n' )   
  49. >   {  
  50. >      putchar( *word );
  51. >      word == word + 1; 
  52. >   }
  53. >}  
  54. >ps: is there a way to find out what exactly error messages mean?  I kept
  55. >getting something similar to: not an Ivalue.  It would be nice if I knew
  56. >what the hell that meant.
  57.  
  58. The message was probably "not an Lvalue" - which means "word" is not something
  59. that  can be put on the left side of an equal sign.
  60.  
  61. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  62. Internet: bennett@triumf.ca         | of one another only when one can be
  63. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  64. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  65. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.